package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Employee user interface:
 * Enables the user to create, find, delete, update and list all employees
 * 
 * @author (Gruppe 3)
 */
public class EmployeeUI
{
    // instance variables 
    private PersonCtr personCtr;

    /**
     * Constructor for objects of class EmployeeUI
     */
    public EmployeeUI()
    {
        // initialise instance variables
        personCtr = new PersonCtr();
    }
    
    /**
     * Opens the employee menu
     */
    public void open()
    {
        employeeMenu();
    }
    
    /**
     * Calls the print employee menu method and let the user pick a choice for the menu
     */
    private void employeeMenu()
    {
        boolean close = false;
        while(!close)
        {
            int choice = makeEmployeeMenu();
            
            if(choice == 1)
            {
                //Creates a new employee
                createEmployee();
            }
            else
            {
                if(choice == 2)
                {
                    //Find a employee
                    findEmployee();
                }
                else
                {
                    if(choice == 3)
                    {
                        //Delete a employee
                        deleteEmployee();
                    }
                    else
                    {
                        if(choice == 4)
                        {
                            //Updates a employee
                            updateEmployee();
                        }
                        else
                        {
                            if(choice == 5)
                            {
                                //Show all employee
                                showAllEmployees();
                            }
                            else
                            {
                                //go back
                                close = true;
                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     * Prints employee menu
     */
    private int makeEmployeeMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ### Medarbejder menu ### ");
        System.out.println(" (1) Opret medarbejder");
        System.out.println(" (2) Find medarbejder");
        System.out.println(" (3) Slet medarbejder");
        System.out.println(" (4) Ret medarbejder");
        System.out.println(" (5) Vis alle medarbejdere");
        System.out.println(" (6) Tilbage");
        System.out.println("\n Vælg et nummer");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }

    /**
     * System waits for an input, so the user can read the screen information
     */
    private void waitInput()
    {
        System.out.println("Tryk på en tast for at forsætte");
        new Scanner(System.in).nextLine();
    }
    
    /**
     * input of a string
     */
    private String inputString()
    {
        Scanner keyboard = new Scanner(System.in);
        String newInput = keyboard.nextLine();
        return newInput;
    }
    
    /**
     * Checks if a string is a double and returns a boolean based on this
     */
        private boolean isDouble(String checkInput)
    {
        try
        {
            Double.parseDouble(checkInput);
        }
        catch(NumberFormatException e)
        {   
            System.out.println("Du skal skrive et heltal eller decimaltal.");
            return false;
        }
        
        return true;        
    } 
    
    /**
     * input of a double
     */
    private double inputDouble()
    {
        Scanner keyboard = new Scanner(System.in);
        double newInput = keyboard.nextDouble();
        return newInput;
    }
    
    /**
     * Create a new employee 
     */
    private void createEmployee()
    {
        System.out.println("Indtast navn");
        String name = inputString();
        
        System.out.println("Indtast adresse");
        String address = inputString();
        
        System.out.println("Indtast Tlf nr.");
        String phone = inputString();
        
        System.out.println("Indtast E-mail");
        String email = inputString();
        
        System.out.println("Indtast ID");
        String employeeID = inputString();
        
       personCtr.addEmployee(name, address, phone, email, employeeID);

        if (!name.isEmpty() && !address.isEmpty() && !phone.isEmpty() && 
            !email.isEmpty() && !employeeID.isEmpty())
        {   
            try
            {   
                if (personCtr.getEmployee(employeeID) == null)
                {
                    personCtr.addEmployee(name, address, phone, email, employeeID);
                    System.out.println("Ansat " + name + " med id: " + employeeID + " er tilføjet");
                    waitInput();
                }
                else 
                {
                    System.out.println("Den ansatte findes allerede i systemet");
                }
                  
            }
            catch(NullPointerException e)
            {
                System.out.println("Du har en eller flere værdier som ikke er gyldige for oprettelse af en ansat"); 
                waitInput();
            }
        }
        else
        {
            System.out.println("Du har ikke indtaste alle værdierne som skal bruges til at oprette en kunde");
            waitInput();
        }

    }
    
    /**
     * Find an employee
     */
    private void findEmployee()
    {
        System.out.println("Indtast medarbejderens ID");
        String searchString = inputString();
        
        if (searchString != null)
        {   
            try
            {
                System.out.println("");
                System.out.println("Navn: " + personCtr.getEmployee(searchString).getName());
                System.out.println("Adresse: " + personCtr.getEmployee(searchString).getAddress());
                System.out.println("Tlf nr.: " + personCtr.getEmployee(searchString).getPhone());
                System.out.println("E-mail: " + personCtr.getEmployee(searchString).getEmail());
                System.out.println("ID: " + personCtr.getEmployee(searchString).getEmployeeID());
                System.out.println("");
                waitInput();
            }
            catch(NullPointerException e )
            {
                System.out.println("Der skete en fejl, da programmet prøvede at finde den ansatte");
                System.out.println("(Måske er den ansat ikke oprettet i systemet)");
                System.out.println("");
                waitInput();
            }
        }
        else 
        {
            System.out.println("Du skal indtaste et id for at finde en ansat");
            waitInput();
        }
        
        
    }
    
    /**
     * Delete an employee
     */
    private void deleteEmployee()
    {
        System.out.println("Indtast Id på medarbejderen");
        String searchID = inputString();
        
        if (searchID != null)
        {   
            try
            {
                personCtr.deleteEmployee(searchID);
                waitInput();
            }
            catch(IndexOutOfBoundsException e)
            {
                System.out.println("System kunne ikke finde og slette den ansatte");
                System.out.println("(Måske er den ansatte ikke registeret?");
                waitInput();
            }
            
        }
        else
        {
            System.out.println("Du skal indtaste et ID for at kunne slette en ansat");
            waitInput();
        }
    }
    
    /**
     * Update an Employee
     */
    private void updateEmployee()
    {
        System.out.println("Indtast ID på medarbejderen");
        String ID = inputString();
        
        System.out.println("Indtast navn");
        String name = inputString();
        
        System.out.println("Indtast Adresse");
        String address = inputString();
        
        System.out.println("Indtast Tlf nr.");
        String phone = inputString();
        
        System.out.println("Indtast E-mail");
        String email = inputString();
        
        System.out.println("Indtast nyt ID");
        String employeeID = inputString();
        
        if (personCtr.getEmployee(ID) != null)
        {   
            try
            {
                personCtr.updateEmployee(name, address, phone, email, employeeID, ID);
                System.out.println("Den ansatte " + name + " med id: " + employeeID + " blev opdateret");
                                   
                waitInput();
            }
            catch(NullPointerException e)
            {
                System.out.println("Du har ikke indtaste alle værdierne, som skal bruges til at opdatere en ansat i systemet");
                waitInput();
            }
        }
        else
        {
            System.out.println("Den ansatte som skulle opdateres kunne ikke findes i systemet");
            waitInput();
        }
    }
    
    /**
     * Show all Employees
     */
    private void showAllEmployees()
    {   
        try 
        {   
            personCtr.listAllEmployees();
            waitInput();
        }
        catch(NullPointerException e )
        {
            System.out.println("Der skete en fejl, da systemet prøvede at liste alle registered ansatte");
            waitInput();
        }
    }
}
